package com.example.javastream.demo;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Stream;

public class MapOps {
    public static void main(String[] args) {
        Stream<Integer> integerStream = Stream.of(1, 2, 3, 4, 5);
        // 将元素类型转换为其他
        Stream<String> stringStream = integerStream.map(i -> i + "1");
        stringStream.forEach(System.out::println);

        System.out.println("1================");
        // 将元素转换为流并合并
        List<Integer> list1 = new ArrayList<Integer>(){{add(1);}};
        List<Integer> list2 = new ArrayList<Integer>(){{add(2);}};
        List<List<Integer>> list = new ArrayList<List<Integer>>(){{add(list1); add(list2);}};

        Stream<List<Integer>> listStream = list.stream(); // 元素是列表
        Stream<Integer> integerStream1 = listStream.flatMap(Collection::stream); // 将元素列表转换为流，并合并多个流为一个
        integerStream1.forEach(System.out::println);
    }
}
